﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for D_Level_Genarator
/// </summary>
namespace Bestlife.ERP.DataAccess
{
    public class D_Level_Genarator
    {
        #region Variables
        List<Bestlife.ERP.Entities.E_AgentToCdiamand> childby_LevelID = new List<Bestlife.ERP.Entities.E_AgentToCdiamand>();
        string Retrun_String;
        string Parent_To_User;
        string Parent_Level;
        string count;
        SqlDataReader rdR;

      //  string Sup_id;
      //  SqlDataReader reader;


        #endregion

        public D_Level_Genarator()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public string Level_Gen(string Sup_ID)
        {




            try
            {
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String Ssql = "SELECT [Parent_To_User],[Level_id] FROM [Distributor_Level] WHERE User_id='" + Sup_ID + "'";
             //   String Ssqln = "SELECT [Parent_To_User],[Level_id] FROM BestlifeDB.Distributor_Level WHERE User_id='" + Sup_ID + "'";

                rdR = clsCon.ReadTable(Ssql);

                if (rdR.Read())
                {
                    Parent_To_User = rdR["Parent_To_User"].ToString();
                    Parent_Level = rdR["Level_id"].ToString();

                    Retrun_String = Parent_To_User + "/" + Parent_Level;

                }
            }
            catch (Exception)
            {
                // Console.WriteLine(exx);
            }

            finally
            {
            rdR.Close();
            }

            return Retrun_String;
            
        }

        public string Child_Count(string Sup_ID)
        {

            SqlConnection conn_new = new SqlConnection();
            try
            {
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String Ssql = "select COUNT(d.Dis_ID) as Rcount from Tbl_Distributor d ,Distributor_Level dl where d.Sup_ID='" + Sup_ID + "' AND dl.User_id=d.Sup_ID";
                //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);

                rdR = clsCon.ReadTable(Ssql);



                if (rdR.Read())
                {
                    count = rdR["RCount"].ToString();

                }
            }
            catch (Exception)
            {
                // Console.WriteLine(exx);
            }
            finally 
            {
                rdR.Close();
            }
            return count;
        }

        #region Getchilds By Level Diamand

        public List<Bestlife.ERP.Entities.E_AgentToCdiamand> GetchildBy_lvlLshipDiamand(string lvlid, string status)
        {
           

            try
            { //try
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

            #region Select Queary

                    String Ssql = "SELECT d.User_id,d.level_id,l.Parent_To_User from Diamond d ,Distributor_Level l where d.Level_id LIKE '" + lvlid + "%' And d.User_id=l.User_id ORDER BY l.Parent_To_User";  
                    rdR = clsCon.ReadTable(Ssql);

                while (rdR.Read())
                {
                    Bestlife.ERP.Entities.E_AgentToCdiamand EAtoCD = new Entities.E_AgentToCdiamand();
                    EAtoCD.Userid= rdR["User_id"].ToString();
                    EAtoCD.Levelid = rdR["level_id"].ToString();
                    EAtoCD.Parent_to_user = rdR["Parent_To_User"].ToString();

                    childby_LevelID.Add(EAtoCD);
                }
            #endregion
              
            } //try

            catch (Exception err)
            {}
            finally
            {
                clsCon.conn_new.Close();
                rdR.Close();

            }   //finally

            return childby_LevelID;
        }
        #endregion

        #region Getchilds By SD

        public List<Bestlife.ERP.Entities.E_AgentToCdiamand> GetchildBy_lvlLshipSDiamand(string lvlid, string status)
        {
            

            try
            { //try
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                    #region Select Queary
                    String Ssql = "SELECT d.User_id,d.level_id,l.Parent_To_User from SD d ,Distributor_Level l where d.Level_id LIKE '" + lvlid + "%' And d.User_id=l.User_id ORDER BY l.Parent_To_User";
                           
                    rdR = clsCon.ReadTable(Ssql);
                    while (rdR.Read())
                    {
                        Bestlife.ERP.Entities.E_AgentToCdiamand EAtoCD = new Entities.E_AgentToCdiamand();
                        EAtoCD.Userid = rdR["User_id"].ToString();
                        EAtoCD.Levelid = rdR["level_id"].ToString();
                        EAtoCD.Parent_to_user = rdR["Parent_To_User"].ToString();

                        childby_LevelID.Add(EAtoCD);
                    }
                    #endregion               

            } //try

            catch (Exception err)
            { }
            finally
            {
                clsCon.conn_new.Close();
                rdR.Close();

            }   //finally

            return childby_LevelID;
        }
        #endregion

        #region Getchilds By SED

        public List<Bestlife.ERP.Entities.E_AgentToCdiamand> GetchildBy_lvlLshipSEDiamand(string lvlid, string status)
        {
            

            try
            { //try
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                    #region Select Queary
                    String Ssql = "SELECT d.User_id,d.level_id,l.Parent_To_User from SED d ,Distributor_Level l where d.Level_id LIKE '" + lvlid + "%' And d.User_id=l.User_id ORDER BY l.Parent_To_User";
                    //String Ssql = "SELECT d.User_id from SED d where d.Level_id LIKE '" + lvlid + "%'";
                    //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);                    
                    rdR = clsCon.ReadTable(Ssql);
                    while (rdR.Read())
                    {
                        Bestlife.ERP.Entities.E_AgentToCdiamand EAtoCD = new Entities.E_AgentToCdiamand();
                        EAtoCD.Userid = rdR["User_id"].ToString();
                        EAtoCD.Levelid = rdR["level_id"].ToString();
                        EAtoCD.Parent_to_user = rdR["Parent_To_User"].ToString();

                        childby_LevelID.Add(EAtoCD);
                    }
                    #endregion              
               
            } //try

            catch (Exception err)
            { }
            finally
            {
                clsCon.conn_new.Close();
                rdR.Close();

            }   //finally

            return childby_LevelID;
        }
        #endregion

        #region Getchilds By CD

        public List<Bestlife.ERP.Entities.E_AgentToCdiamand> GetchildBy_lvlLshipCDiamand(string lvlid, string status)
        {
           

            try
            { //try
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                    #region Select Queary

                    String Ssql = "SELECT d.User_id,d.level_id,l.Parent_To_User from C_Diamond d ,Distributor_Level l where d.Level_id LIKE '" + lvlid + "%' And d.User_id=l.User_id ORDER BY l.Parent_To_User";

                    //String Ssql = "SELECT d.User_id from C_Diamond d where d.Level_id LIKE '" + lvlid + "%'";
                    //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);                    
                    rdR = clsCon.ReadTable(Ssql);
                    while (rdR.Read())
                    {
                        Bestlife.ERP.Entities.E_AgentToCdiamand EAtoCD = new Entities.E_AgentToCdiamand();
                        EAtoCD.Userid = rdR["User_id"].ToString();
                        EAtoCD.Levelid = rdR["level_id"].ToString();
                        EAtoCD.Parent_to_user = rdR["Parent_To_User"].ToString();

                        childby_LevelID.Add(EAtoCD);
                    }
                    #endregion
                
            } //try

            catch (Exception err)
            { }
            finally
            {
                clsCon.conn_new.Close();
                rdR.Close();

            }   //finally

            return childby_LevelID;
        }
        #endregion


        public List<string> Return_ActList(List<Bestlife.ERP.Entities.E_AgentToCdiamand> inlist)
        {
            List<string> listids = new List<string>();
            List<Bestlife.ERP.Entities.E_AgentToCdiamand> getlist = inlist;
            try
            {
               

                int chpcount = getlist.Count;
                for (int i = 0; i < chpcount; i++)
                {

                    #region Check childs and Remove

                    lock (getlist)
                    {
                        foreach (Bestlife.ERP.Entities.E_AgentToCdiamand childdetail in getlist)
                        {

                            if (childdetail.Levelid.ToString().Contains(getlist[i].Levelid.ToString()))
                            {

                                if (!(getlist[i].Userid.ToString().Equals(childdetail.Userid.ToString())))
                                {
                                    getlist.Remove(childdetail);
                                }
                                else 
                                { 
                                    listids.Add(getlist[i].Userid.ToString());//user id--> add to list  
                                }

                            }

                        }
                    }

                    #endregion
                }
               
            }
            catch(Exception re)
            {
            
            }
            return listids;
        }
    }
}